﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using Model;
using System.Data;
using Utility;
using System.Data.SqlClient;
namespace Manager
{
    /// <summary>
    /// HW_School业务逻辑
    /// 创建人：Wilson
    /// 创建时间：2013-1-29  
    /// </summary>
    public class HW_SchoolManager
    {
        			
        #region##通过主键ID得到HW_School
        /// <summary>
        /// 功能：通过ID得到HW_School
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
        public HW_School GetById(string schoollcode)
        {            
            using (HW_SchoolService ws = new HW_SchoolService())
			{
				return ws.GetById(schoollcode);
			}
        }
        public DataTable GetByDistrict(int pageIndex, int pageSize, out int count, string DistrictId,string searchString)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                return ws.GetByDistrict(pageIndex, pageSize, out count, DistrictId, searchString);
            }
        }
        public List<HW_School> GetAPISchool(string district)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                return ws.GetAPISchool(district).ToList();
            }
        }

        #endregion
		
		#region##通过主键ID删除HW_School
        /// <summary>
        /// 功能：通过主键ID删除HW_School
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29   
        /// </summary>
        /// <param name="">主键ID</param>
        /// <returns></returns>
		public int DeleteById(string schoollcode)
		{
			using (HW_SchoolService ws = new HW_SchoolService())
			{
                SqlConnection conn = new SqlConnection(WebConn.connString);
                if (conn.State == System.Data.ConnectionState.Closed)
                    conn.Open();
                SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    HW_School_UserService schoolUserService = new HW_School_UserService();
                    schoolUserService.DeleteBySchoolCode(trans, schoollcode);
                    int n= ws.DeleteById(trans, schoollcode);
                    trans.Commit();
                    return n;
                }
                catch(Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
			}
		}
		#endregion			
		
        #region##添加HW_School
        /// <summary>
        /// 功能：添加HW_School
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int Add(HW_School hw_school)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
			{
				return ws.Add(hw_school);
			}
        }
        //public void Add(HW_School hw_school,bool addUser,SqlTransaction trans)
        //{
        //    using (HW_SchoolService ws = new HW_SchoolService())
        //    {
                
        //        ws.Add(trans,hw_school);
        //        if (addUser)
        //        {
        //            HW_DistrictManager dismanager = new HW_DistrictManager();
        //            HW_School_User user = new HW_School_User();

        //            HW_Manger_Admin_UserManager adminmanager = new HW_Manger_Admin_UserManager();
        //            var dt = dismanager.GetManager(hw_school.DistrictCode);

        //            string dis = dt.Rows[0]["XX_UserName"] as string;
        //            dis = dis.Substring(0, 3);
        //            HW_School_UserManager schoolusermanaer = new HW_School_UserManager();

        //            string maxuser = schoolusermanaer.GetMaxUser(dis);
        //            if (string.IsNullOrEmpty(maxuser))
        //            {
        //                maxuser = dis + "10001";
        //            }
        //            string username = string.Empty;
        //            username = dis + (Convert.ToInt32(maxuser.Substring(3)) + 1).ToString();

        //            user.XX_UserName = username;
        //            user.CNName = hw_school.LinkMan;
        //            user.SchoollCode = hw_school.SchoollCode;
        //            user.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
        //            schoolusermanaer.Add(user);
        //        }
        //    }
        //}
/// <summary>
/// 增加学校
/// </summary>
/// <param name="hw_school">学校</param>
/// <param name="permit">是否授权</param>
/// <param name="addNewUser">是否增加默认学校用户</param>
        public void Add(HW_School hw_school,bool permit,bool addNewUser)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(Utility.WebConn.connString);
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                }
                System.Data.SqlClient.SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    hw_school.Statute = "T";
                    hw_school.Permit = permit;
                    //增加学校
                     ws.Add(trans,hw_school);
                    //增加学校用户
                     if (addNewUser)
                     {
                         HW_DistrictService DistrictService = new HW_DistrictService();
                         HW_School_UserService School_UserService = new HW_School_UserService();
                         HW_School_User user = new HW_School_User();
                         var dt = DistrictService.GetManager(hw_school.DistrictCode);
                         string dis = dt.Rows[0]["XX_UserName"] as string;
                         dis = dis.Substring(0, 3);
                         string maxuser = School_UserService.GetMaxUser(dis);
                         if (string.IsNullOrEmpty(maxuser))
                         {
                             maxuser = dis + "10001";
                         }
                         string username = string.Empty;
                         username = dis + (Convert.ToInt32(maxuser.Substring(3)) + 1).ToString();

                         user.XX_UserName = username;
                         user.CNName = hw_school.LinkMan;
                         user.SchoollCode = hw_school.SchoollCode;
                         user.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                         user.RoleId = 3000;
                         //增加网站登录用户
                         HW_XXX_UserService xxxservice = new HW_XXX_UserService();
                         HW_XXX_User xxxuser = new HW_XXX_User()
                         {
                             XX_UserName = user.XX_UserName,
                             XX_Pass = user.XX_Pass,
                             RoleId = 3000,
                             CNName = user.CNName,
                         };
                         xxxservice.Add(trans, xxxuser);
                         //增加学校用户
                         School_UserService.Add(trans,user);
                     }

                    //Add(hw_school, addNewUser,trans);
                    //增加授权
                    if (permit)
                    {
                        hw_school.Permit = permit;
                        ChangePermit(hw_school,trans);
                        //Permit(hw_school, trans);
                    }
                    trans.Commit();
                }
                catch(Exception e)
                {
                    trans.Rollback();
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
        public void Add(HW_School hw_school, bool permit, bool addNewUser,SqlTransaction trans)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                try
                {
                    hw_school.Statute = "T";
                    //增加学校
                    ws.Add(trans, hw_school);
                    //增加学校用户
                    if (addNewUser)
                    {
                        HW_DistrictService DistrictService = new HW_DistrictService();
                        HW_School_UserService School_UserService = new HW_School_UserService();
                        HW_School_User user = new HW_School_User();
                        var dt = DistrictService.GetManager(hw_school.DistrictCode);
                        string dis = dt.Rows[0]["XX_UserName"] as string;
                        dis = dis.Substring(0, 3);
                        string maxuser = School_UserService.GetMaxUser(dis);
                        if (string.IsNullOrEmpty(maxuser))
                        {
                            maxuser = dis + "10001";
                        }
                        string username = string.Empty;
                        username = dis + (Convert.ToInt64(maxuser.Substring(3)) + 1).ToString();

                        user.XX_UserName = username;
                        user.CNName = hw_school.LinkMan;
                        user.SchoollCode = hw_school.SchoollCode;
                        user.XX_Pass = System.Configuration.ConfigurationManager.AppSettings["default"].ToString();
                        user.RoleId = 3000;
                        //增加网站登录用户
                        HW_XXX_UserService xxxservice = new HW_XXX_UserService();
                        HW_XXX_User xxxuser = new HW_XXX_User()
                        {
                            XX_UserName = user.XX_UserName,
                            XX_Pass = user.XX_Pass,
                            RoleId = 3000,
                            CNName = user.CNName,
                        };
                        xxxservice.Add(trans, xxxuser);
                        //增加学校用户
                        School_UserService.Add(trans, user);
                    }

                    //Add(hw_school, addNewUser,trans);
                    //增加授权
                    if (permit)
                    {
                        hw_school.Permit = permit;
                        Permit(hw_school, trans);
                    }
                }
                catch(Exception ex)
                {
                    throw ex;
                }
            }
        }
        //public void Add(HW_School hw_school,SqlTransaction trans,bool permit)
        //{
        //    using (HW_SchoolService ws = new HW_SchoolService())
        //    {
                
        //        try
        //        {

        //            ws.Add(trans,hw_school);
        //            if (permit)
        //            {
        //                hw_school.Permit = permit;
        //                Permit(hw_school, trans);
        //            }
        //        }
        //        catch
        //        {
                    
        //        } 
        //    }
        //}
        public int ChangeState(string schoolid,string state,SqlTransaction tans)
        {
            HW_SchoolService ws = new HW_SchoolService();
            return ws.ChangeState(schoolid,state,tans);
        }
        #endregion
		
		#region##得到HW_School集合
		/// <summary>
        /// 功能：得到HW_School集合
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>       
        /// <returns></returns>
		public IList<HW_School> GetList()
		{
		    using (HW_SchoolService ws = new HW_SchoolService())
			{
				return ws.GetList();
			}
		}
		#endregion
		
		#region##修改HW_School
        /// <summary>
        /// 功能：修改HW_School
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int ModHW_School(HW_School hw_school)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
			{
				return ws.ModHW_School(hw_school);
			}
        }
        public int Update(string schoolcode,string filed, string value)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                return ws.Update(schoolcode,filed, value);
            }
        }
        #endregion
		
		#region##统计HW_School
		/// <summary>
        /// 修改HW_School
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
		public int SelectCount()
		{
			using (HW_SchoolService ws = new HW_SchoolService())
			{
				return ws.SelectCount();
			}
		}
        /// <summary>
        /// 修改HW_School
        /// 创建人：  Wilson 
        /// 创建时间：2013-1-29
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public int SelectCountById()
        {
            using (HW_SchoolService ws = new HW_SchoolService())
			{
				return ws.SelectCountById();
			}
        }
		#endregion
        #region##自定义HW_School
        public int Permit(HW_School school) 
        { 
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                //RSAHelper rsa = new RSAHelper();
                //rsa.Initial(school.SchoollCode);
                XMLHelper xmlHelper = new XMLHelper();
                if (school.Permit.HasValue && school.Permit.Value)
                {
                    //xmlHelper.CreateSignFile(school);
                    xmlHelper.CreateValidateFile(school);
                }
                else
                {
                    xmlHelper.DeleteSignFile(school);
                }
                int i = ws.ChangePermit(school.SchoollCode,school.Permit.Value);
                return i;
            }
        }
        public int Permit(HW_School school,SqlTransaction trans)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                RSAHelper rsa = new RSAHelper();
                rsa.Initial(school.SchoollCode);
                XMLHelper xmlHelper = new XMLHelper();
                if (school.Permit.HasValue && school.Permit.Value)
                {
                    xmlHelper.CreateSignFile(school);
                }
                else
                {
                    xmlHelper.DeleteSignFile(school);
                }
                //SqlConnection conn = new SqlConnection(Utility.WebConn.connString);
                //if (conn.State == System.Data.ConnectionState.Closed)
                //    conn.Open();
                //SqlTransaction trans = conn.BeginTransaction();
                try
                {
                    int i = ws.ChangePermit(school.SchoollCode, school.Permit.Value, trans);
                    return i;
                }
                catch
                {
                    trans.Rollback();
                    return 0;
                }
            }
        }
        public int ChangePermit(HW_School school, SqlTransaction trans)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                try
                {
                    int i = ws.ChangePermit(school.SchoollCode, school.Permit.Value, trans);
                    return i;
                }
                catch
                {
                    return 0;
                }
            }
        }
        public int ChangePermit(HW_School school)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                try
                {
                    int i = ws.ChangePermit(school.SchoollCode, school.Permit.Value);
                    return i;
                }
                catch
                {
                    return 0;
                }
            }
        }
        public DataTable GetSchoolDetail(int pageIndex, int pageSize, out int count,string district)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                var result= ws.GetSchoolDetail(pageIndex, pageSize, out count,district);
                return ShowState(result);
            }
        }
        public DataTable Search(int pageIndex,int pageSize,out int count,string district,string searchString)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                return ShowState(ws.Search(pageIndex, pageSize,out count,district, searchString));
            }
        }
        /// <summary>
        /// 根据学校统计上传
        /// </summary>
        /// <param name="year"></param>
        /// <param name="orgCode"></param>
        /// <returns></returns>
        public DataTable GetMySchool(string year,string orgCode)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                return ws.GetMySchool(year,orgCode);
            }
        }
        public DataTable GetDistrictSchool(string year, string districtid)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                return ws.GetDistrictSchool(year, districtid);
            }
        }
        public DataTable GetSchoolByOrgCode(int pageIndex, int pageSize, out int count, string orgCode)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                var result= ws.GetSchoolByOrgCode( pageIndex,  pageSize, out  count,  orgCode);
                return ShowState(result);
        
            }
        }
        public DataTable GetSchoolBySchoolCode(int pageIndex, int pageSize, out int count, string SchoolCode)
        {
            using (HW_SchoolService ws = new HW_SchoolService())
            {
                var result= ws.GetSchoolBySchoolCode(pageIndex, pageSize, out  count, SchoolCode);
                return ShowState(result);

            }
        }
        public DataTable ShowState(DataTable dt)
        {
            for(int i=0;i<dt.Rows.Count;i++)
            {
                if (dt.Rows[i]["Statute"] as string == "T")
                {
                    dt.Rows[i]["Statute"] = "正常";
                }
                else if (dt.Rows[i]["Statute"] as string == "F")
                {
                    dt.Rows[i]["Statute"] = "合并";
                }
                else
                {
                    dt.Rows[i]["Statute"] = "停办";
                }
            }
            return dt;
        }
        #endregion
    }
}